package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sky.constant.MessageConstant.DISH_ON_SALE;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //套餐分页查询
    @Override
    public PageResult list(SetmealPageQueryDTO dto) {
        //设置启动参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //调用mapper层
        Page<SetmealVO> result = setmealMapper.list(dto);
        return new PageResult(result.getTotal(), result.getResult());
    }

    //新增套餐
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO dto) {
        //对两个表进行插入操作\
//        插入套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.add(setmeal);
        //插入套餐菜品关系表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        log.info("setmealDishes:{}", setmealDishes);
        setmealDishMapper.add(setmealDishes);

    }

    //根据id查询套餐
    @Override
    @Transactional
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmeal = setmealMapper.getSetmealById(id);
        List<SetmealDish> list = setmealMapper.getSetmealDishBuId(id);
        setmeal.setSetmealDishes(list);
        return setmeal;
    }
    //修改套餐
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO dto) {
        //更新套餐基本数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.updateSetmeal(setmeal);
        //更新套餐对应的菜品信息（先删后增）
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        //根据套餐id删除setmeal_dish

        setmealDishMapper.deleteById(dto.getId());

        setmealDishMapper.add(setmealDishes);
    }
    //批量删除套餐
    @Override
    public void deleteSetmealById(List<Long> ids) {
        //停售状态的套餐才能进行删除操作
        ids.forEach(setmealId -> {
            SetmealVO setmeal = getSetmealById(setmealId);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(DISH_ON_SALE);
            }
        });
        setmealMapper.deleteById(ids);//批量删除操作
    }
    //修改套餐状态
    @Override
    public void updateSetmealStatus(Integer status,Long id) {
        setmealMapper.updateSetmealStatus(status,id);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.newList(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
